#include #include #include #include #include #define MAX_WORD_SIZE 30 #define MAX_MEANING_SIZE 30 #define PRINT_SIZE 1000 #define MAX_ELEMENT 3000 #define INSERT_WORD 1 #define DELETE_WORD 2 #define DISPLAY 3 #define WORD_SEARCH 4 #define DATE_WORD_DISPLAY 5 #define MIN_COUNT_DISPLAY 6 #define MAX_COUNT_DISPLAY 7 #define GAME_PLAY 8 #define EXIT 9 enum COLOR {RED,BLACK}; typedef enum COLOR COLOR; typedef struct DATA{ int date; char word[MAX_WORD_SIZE]; char meaning[MAX_MEANING_SIZE]; int count; }DATA; typedef struct Heaptype{ DATA heap[MAX_ELEMENT]; int heap_size; }Heaptype; typedef struct rbtree{ DATA key; COLOR color; struct rbtree *left; struct rbtree *right; struct rbtree *p; }ELEM_RB; typedef ELEM_RB *RBTREE; //Àü¿ªº¯¼ö RBTREE nil;// nil ³ëµå¸¦ µû·Î ¸¸µå´Â ÀÌÀ¯´Â BLACK ¼Ó¼ºÀ» °¡Áö±â ¶§¹®ÀÌ´Ù. DATA NULL_DATA; // ; int print_count; int game_num = 0; //ÇÔ¼ö RBTREE left_rotate(RBTREE tree, RBTREE base);// ÁÂȸÀü ÇÔ¼ö RBTREE right_rotate(RBTREE tree, RBTREE base);// ¿ìȸÀü ÇÔ¼ö RBTREE RB_insert_fixup(RBTREE tree, RBTREE input);//»ðÀÔ ÇȾ÷ÇÔ¼ö RBTREE RB_insertword(RBTREE tree, RBTREE input);// »ðÀÔ ÇÔ¼ö(°Ë»öÀ» À§ÇØ) RBTREE RB_insertfre(RBTREE tree,RBTREE input);//ºóµµ¼ö¸¦ À§ÇØ RBTREE RB_insertdate(RBTREE tree,RBTREE input);//ÀÔ·ÂÇÑ ¼ø¼­¸¦ À§ÇØ RBTREE make_node(DATA input);// ³ëµå »ý¼º ÇÔ¼ö RBTREE make_nil(); // nil ³ëµå »ý¼º ÇÔ¼ö RBTREE tree_minimum(RBTREE root); ; RBTREE tree_successor(RBTREE root); RBTREE RB_delete_fixup(RBTREE tree, RBTREE put); RBTREE RB_delete(RBTREE tree, RBTREE put); RBTREE tree_searchword(RBTREE tree, DATA d); RBTREE tree_searchdate(RBTREE tree, int d); void print_inorder(RBTREE tree,RBTREE entree); void delete_all(RBTREE tree); void emptydata(DATA a); void print_inorder_end(RBTREE tree, FILE *f); void print_inorderdate(RBTREE tree,RBTREE entree, int date); void print_inorder_word(RBTREE tree,DATA mindata,DATA maxdata); void insert_max_heap(Heaptype *h,DATA item); void insert_min_heap(Heaptype *h,DATA item); DATA delete_max_heap(Heaptype *h); DATA delete_min_heap(Heaptype *h); void display(RBTREE tree); void insert_value(RBTREE tree,Heaptype *minheap,Heaptype *maxheap); void heap_init(Heaptype *minheap,Heaptype *maxheap); void insert_game_array(RBTREE tree,DATA *d); int main(void) { FILE *engfile = fopen("eng1.txt","r"); //±¸Á¶Ã¼ ¼±¾ð Heaptype minheap,maxheap; RBTREE englishtree; RBTREE frequencytree; RBTREE datetree; RBTREE val; DATA temp,temp1,temp2; DATA game[MAX_ELEMENT]; //º¯¼ö ¼±¾ð int date; int num = 0; int year,month,day; int choice = 0; int tempn; int life = 0; //initializing time_t timer; struct tm *t; //ÃʱâÈ­ minheap.heap_size = 0; maxheap.heap_size = 0; emptydata(NULL_DATA); nil = make_nil(); englishtree = nil; frequencytree = nil; datetree = nil; timer = time(NULL); // ÇöÀç ½Ã°¢À» ÃÊ ´ÜÀ§·Î ¾ò±â t = localtime(&timer); // ÃÊ ´ÜÀ§ÀÇ ½Ã°£À» ºÐ¸®ÇÏ¿© ±¸Á¶Ã¼¿¡ ³Ö±â date = ((t->tm_year-100) * 10000000 ) + ((t->tm_mon+1) * 100000) + (t->tm_mday * 1000); while(feof(engfile)==0){ fscanf(engfile,"%d %s %s %d ",&temp1.date,&temp1.word,&temp1.meaning,&temp1.count); if((date <= temp1.date) &&(temp1.date < (date + 1000)))num++; // ÇöÀç ³¯Â¥ val = make_node(temp1); //³ëµå¸¦ ¸¸µé°í englishtree = RB_insertword(englishtree,val);//Æ®¸®¿¡ ³Ö°í // val = make_node(temp1); //frequencytree = RB_insertfre(frequencytree,val); val = make_node(temp1); datetree = RB_insertdate(datetree,val); insert_max_heap(&maxheap,temp1); insert_min_heap(&minheap,temp1); } fclose(engfile); /*print_count = 15; print_inorder(frequencytree,englishtree); printf("¾Æ¹«Å°³ª ´­·¯ÁÖ¼¼¿ä ¸Þ´ºÈ­¸éÀ¸·Î ³Ñ¾î°©´Ï´Ù.\n");*/ printf("¿À´ÃÀÇ ¾Ï±â´Ü¾î\n"); for(tempn = 0; tempn < 15;tempn++){ temp2 = delete_min_heap(&minheap); printf("´Ü¾î:%s ¶æ:%s ºóµµ¼ö:%d\n",temp2.word,temp2.meaning,temp2.count); tree_searchword(englishtree,temp2)->key.count++; } heap_init(&minheap,&maxheap); insert_value(englishtree,&minheap,&maxheap); printf("¾Æ¹«Å°³ª ´­·¯ÁÖ¼¼¿ä ¸Þ´ºÈ­¸éÀ¸·Î ³Ñ¾î°©´Ï´Ù.\n"); getchar(); system("cls"); do{ printf("¦£¦¡¦¡¦¡¦¡¦¡¦¡¦¡¦¡¦¡¦¤\n"); printf("¦¢1.´Ü¾î ÀÔ·Â ¦¢\n"); printf("¦¢2.´Ü¾î »èÁ¦ ¦¢\n"); printf("¦¢3.Àüü Ãâ·Â ¦¢\n"); printf("¦¢4.´Ü¾î ã±â ¦¢\n"); printf("¦¢5.ÀÔ·Â ³¯Â¥ ´Ü¾î ¦¢\n"); printf("¦¢6.ºóµµ¼ö ³·Àº ´Ü¾î¦¢\n"); printf("¦¢7.ºóµµ¼ö ³ôÀº ´Ü¾î¦¢\n"); printf("¦¢8.´Ü¾î °ÔÀÓ ¦¢\n"); printf("¦¢9.Á¾·á ¦¢\n"); printf("¦¦¦¡¦¡¦¡¦¡¦¡¦¡¦¡¦¡¦¡¦¥\n"); printf("\n>"); scanf("%d",&choice); switch(choice){ case INSERT_WORD: num += date; temp.date = num; printf("\n´Ü¾î¸¦ ÀÔ·ÂÇÕ´Ï´Ù.\n"); scanf("%s",temp.word); printf("\n¶æÀ» ÀÔ·ÂÇÕ´Ï´Ù.\n"); scanf("%s",temp.meaning); temp.count = 0; printf("input: %s\n", temp.word); val = make_node(temp); //³ëµå¸¦ ¸¸µé°í englishtree = RB_insertword(englishtree,val);//Æ®¸®¿¡ ³Ö°í //val = make_node(temp); //frequencytree = RB_insertfre(frequencytree,val); val = make_node(temp); datetree = RB_insertdate(datetree,val); // print_inorder(englishtree); //Ãâ·Â insert_min_heap(&minheap,temp); insert_max_heap(&maxheap,temp); printf("\n"); num -= date; // ¿À´Ã ³¯Â¥¸¦ »­ num++;//´Ü¾î Çϳª Ãß°¡ ÇßÀ¸´Ï ++ break; case DELETE_WORD: printf("\n´Ü¾î¸¦ ÀÔ·ÂÇÕ´Ï´Ù.\n"); scanf("%s",temp.word); val = tree_searchword(englishtree,temp); //³ëµå¸¦ ã¾Æº¸°í if(val != nil) { printf("delete: %s\n", temp.word); englishtree = RB_delete(englishtree,val); //ÀÖÀ»¶§ Æ®¸®¿¡¼­ Áö¿ì°í // print_inorder(englishtree); // Ãâ·Â printf("\n"); heap_init(&minheap,&maxheap); insert_value(englishtree,&minheap,&maxheap);break; }else{ printf("'%s' is not exist in tree \n", temp.word); } break; case DISPLAY://Àüü Ãâ·Â display(englishtree);break; case WORD_SEARCH://´Ü¾î °Ë»ö printf("\n´Ü¾î¸¦ ÀÔ·ÂÇÕ´Ï´Ù.\n"); scanf("%s",temp.word); val = tree_searchword(englishtree,temp); if(val != nil){ printf("¶æÀº %s ÀÔ´Ï´Ù.\n",val->key.meaning); val->key.count++; }else{ printf("%s °¡ Á¸ÀçÇÏÁö ¾Ê½À´Ï´Ù.\n",temp.word); int n = strlen(temp.word); DATA maxdata = temp; maxdata.word[n] = '{'; maxdata.word[n+1] = NULL; printf("[[[[°ü·Ã ´Ü¾î]]]]\n"); print_inorder_word(englishtree,temp,maxdata); } break; case DATE_WORD_DISPLAY://³âµµ Ãâ·Â printf("Ãâ·ÂÇÏ°í ½ÍÀº ³¯Â¥¸¦ ÀÔ·ÂÇϽÿÀ.\n"); printf("³âµµ(20__)? ");scanf("%d",&year); printf("__¿ù? ");scanf("%d",&month); printf("__ÀÏ? ");scanf("%d",&day); date = (year * 10000000 ) + (month * 100000) + (day*1000); print_inorderdate(datetree,englishtree,date); break; //case 6://ºóµµ¼ö Ãâ·Â(Æ®¸®ÀÌ¿ë) // print_count = 15; // print_inorder(frequencytree,englishtree);break; case MIN_COUNT_DISPLAY: //ÃÖÀú for(tempn = 0; tempn < 15;tempn++){ temp2 = delete_min_heap(&minheap); printf("´Ü¾î:%s ¶æ:%s ºóµµ¼ö:%d\n",temp2.word,temp2.meaning,temp2.count); tree_searchword(englishtree,temp2)->key.count++; } heap_init(&minheap,&maxheap); insert_value(englishtree,&minheap,&maxheap); break; case MAX_COUNT_DISPLAY: //ÃÖ´ë for(tempn = 0; tempn < 15;tempn++){ temp2 = delete_max_heap(&maxheap); printf("´Ü¾î:%s ¶æ:%s ºóµµ¼ö:%d\n",temp2.word,temp2.meaning,temp2.count); tree_searchword(englishtree,temp2)->key.count++; } heap_init(&minheap,&maxheap); insert_value(englishtree,&minheap,&maxheap); break; case GAME_PLAY: srand((unsigned int)time(NULL)); life = 100; game_num = 0; insert_game_array(englishtree, game); do{ temp = game[rand()%(game_num)]; printf("¶æ: %s ¿¡ ÇØ´çÇÏ´Â ´Ü¾î¸¦ ÀÔ·ÂÇϽÿÀ.(Á¾·á´Â 0À» ÀÔ·ÂÇϼ¼¿ä)\n",temp.meaning); scanf("%s",temp1.word); if(temp1.word[0] == '0'){ //Á¾·á printf("°ÔÀÓÀ» Á¾·áÇÕ´Ï´Ù.\n"); break; } if(strcmp(temp.word,temp1.word) == 0){ printf("Á¤´äÀÔ´Ï´Ù.(life:%d)\n",life); }else{ life -= 10; printf("¿À´äÀÔ´Ï´Ù.(life:%d)\n",life); printf("Á¤´ä : %s \n",temp.word); } if(life <= 0)break; }while(1); break; case EXIT://Á¾·á break; } if(choice != 9){ printf("¾Æ¹«Å°³ª ´­·¯ÁÖ¼¼¿ä ´ÙÀ½ ÀÛ¾÷À¸·Î ³Ñ¾î°©´Ï´Ù.\n"); getchar();getchar(); system("cls"); } engfile = fopen("eng1.txt","w"); print_inorder_end(englishtree,engfile); fclose(engfile); } while (choice != 9); // µ¿Àû ÇÒ´ç ¸ðµÎ ÇìÁ¦ delete_all(englishtree); delete_all(frequencytree); delete_all(datetree); free(nil); return 0; } void emptydata(DATA a){ a.count = 0; a.date = 0; a.word[0] = NULL; a.meaning[0] = NULL; } RBTREE left_rotate(RBTREE tree, RBTREE base){ RBTREE y = base -> right; base -> right = y -> left; if (y -> left != nil) y -> left -> p = base; y -> p = base -> p; if (base -> p == nil){ tree = y; } else if (base == base -> p -> left){ base -> p -> left = y; } else{ base -> p -> right = y; } y -> left = base; base -> p = y; return tree; } RBTREE right_rotate(RBTREE tree, RBTREE base){// ¿ìȸÀü ÇÔ¼ö RBTREE y = base -> left; base -> left = y -> right; if (y -> right != nil) y -> right -> p = base; y ->p = base -> p; if (base -> p == nil){ tree = y; } else if (base == base -> p -> right){ base -> p -> right = y; } else{ base -> p -> left = y; } y -> right = base; base -> p = y; return tree; } RBTREE RB_insert_fixup(RBTREE tree, RBTREE input){ RBTREE y; while ( input -> p -> color == RED) { if( (input -> p) == (input -> p -> p -> left)) { y = input -> p -> p -> right; if (y -> color == RED) { input -> p -> color = BLACK; y -> color = BLACK; input -> p -> p -> color = RED; input = input -> p -> p; } else { if ( input == input -> p -> right) { input = input -> p; tree = left_rotate(tree,input); } input -> p -> color = BLACK; input -> p -> p -> color = RED; tree = right_rotate(tree,input->p->p); } } else { y = input -> p -> p -> left; if (y -> color == RED) { input -> p -> color = BLACK; y -> color = BLACK; input -> p -> p -> color = RED; input = input -> p -> p; } else { if ( input == input -> p -> left) { input = input -> p; tree = right_rotate(tree,input); } input -> p -> color = BLACK; input -> p -> p -> color = RED; tree = left_rotate(tree,input->p->p); } } } tree -> color = BLACK; return tree; } RBTREE RB_insertword(RBTREE tree, RBTREE input){// »ðÀÔ ÇÔ¼ö RBTREE y = nil; RBTREE x = tree; while (x != nil) { y = x; if (strcmp((input -> key.word), (x -> key.word)) < 0){ x = x -> left; } else{ x = x -> right; } } input -> p = y; if (y == nil){ tree = input; } else if ((strcmp((input -> key.word), (y -> key.word)) < 0)){ y -> left = input; } else{ y -> right = input; } input -> left = nil; input -> right = nil; input -> color = RED; return RB_insert_fixup(tree,input); } RBTREE RB_insertfre(RBTREE tree, RBTREE input){// »ðÀÔ ÇÔ¼ö RBTREE y = nil; RBTREE x = tree; while (x != nil) { y = x; if ((input->key.count) < (x->key.count)){ x = x -> left; } else{ x = x -> right; } } input -> p = y; if (y == nil){ tree = input; } else if ((input->key.count) < (y->key.count)){ y -> left = input; } else{ y -> right = input; } input -> left = nil; input -> right = nil; input -> color = RED; return RB_insert_fixup(tree,input); } RBTREE RB_insertdate(RBTREE tree, RBTREE input){// »ðÀÔ ÇÔ¼ö RBTREE y = nil; RBTREE x = tree; while (x != nil) { y = x; if ((input->key.date) < (x->key.date)){ x = x -> left; } else{ x = x -> right; } } input -> p = y; if (y == nil){ tree = input; } else if ((input->key.date) < (y->key.date)){ y -> left = input; } else{ y -> right = input; } input -> left = nil; input -> right = nil; input -> color = RED; return RB_insert_fixup(tree,input); } RBTREE make_node(DATA input){ RBTREE tmp; tmp = (RBTREE)malloc(sizeof(ELEM_RB)); tmp ->key = input; tmp ->left = nil; tmp -> right = nil; tmp ->p = nil; return tmp; } RBTREE make_nil() // nil ³ëµå »ý¼º ÇÔ¼ö { RBTREE tmp; tmp = (RBTREE)malloc(sizeof(ELEM_RB)); tmp ->key = NULL_DATA; tmp ->left = NULL; tmp -> right = NULL; tmp ->p = NULL; tmp ->color = BLACK; return tmp; } RBTREE tree_minimum(RBTREE root){ RBTREE tmp = root; while (tmp -> left != nil) { tmp = tmp -> left; } return tmp; } RBTREE tree_successor(RBTREE root){ RBTREE tmp; if (root -> right != nil) return tree_minimum(root -> right); tmp = root->p; while((tmp != nil) && (root == tmp->right)) { root = tmp; tmp = tmp -> p; } return tmp; } RBTREE RB_delete_fixup(RBTREE tree, RBTREE put){ RBTREE w; while((put != tree) &&(put -> color == BLACK)) { if(put == put -> p -> left) { w = put -> p -> right; if (w -> color == RED) { w -> color = BLACK; put -> p -> color = RED; tree = left_rotate(tree,put->p); w = put -> p -> right; } if ((w->left->color == BLACK)&&(w->right->color == BLACK)) { w -> color = RED; put = put -> p; } else { if(w -> right -> color == BLACK) { w->left->color = BLACK; w-> color = RED; tree = right_rotate(tree,w); w = put->p->right; } w -> color = put -> p -> color; put -> p -> color = BLACK; w -> right -> color = BLACK; tree = left_rotate(tree,put->p); put = tree; } } else { w = put -> p -> left; if (w -> color == RED) { w -> color = BLACK; put -> p -> color = RED; tree = right_rotate(tree,put->p); w = put -> p -> left; } if ((w->right->color == BLACK)&&(w->left->color == BLACK)) { w -> color = RED; put = put -> p; } else { if(w -> left -> color == BLACK) { w->right->color = BLACK; w-> color = RED; tree = left_rotate(tree,w); w = put->p->left; } w -> color = put -> p -> color; put -> p -> color = BLACK; w -> left -> color = BLACK; tree = right_rotate(tree,put->p); put = tree; } } } put -> color = BLACK; return tree; } RBTREE RB_delete(RBTREE tree, RBTREE put){ RBTREE y; RBTREE x; if ((put -> left == nil)||(put->right == nil)){ y = put; } else{ y = tree_successor(put); } if(put -> left != nil){ x = y -> left; } else{ x = y -> right; } x->p = y->p; if (y -> p == nil){ tree = x; } else { if (y == y -> p -> left){ y -> p -> left = x; } else{ y -> p -> right = x; } } if ( y != put){ put -> key = y -> key; } if (y -> color == BLACK){ tree = RB_delete_fixup(tree,x); } return tree; } RBTREE tree_searchword(RBTREE tree, DATA d){ RBTREE root; root = tree; while ((root != nil) && (strcmp(d.word, root -> key.word) != 0)) { if (strcmp(d.word, root -> key.word) == -1){ root = root -> left; } else{ root = root -> right; } } return root; } RBTREE tree_searchdate(RBTREE tree, int d){ RBTREE root; root = tree; while ((root != nil) && d != root -> key.date ) { if (d < root -> key.date){ root = root -> left; } else{ root = root -> right; } } return root; } void insert_game_array(RBTREE tree,DATA *d){ if(tree != nil) { insert_game_array(tree->left, d); d[game_num++] = tree->key; insert_game_array(tree->right, d); } } void insert_value(RBTREE tree,Heaptype *minheap,Heaptype *maxheap){ if(tree != nil) { insert_value(tree->left, minheap,maxheap); insert_min_heap(minheap,tree->key); insert_max_heap(maxheap,tree->key); insert_value(tree->right, minheap,maxheap); } } void display(RBTREE tree){// INORDER ·Î Ž»öÇϸ鼭 ±íÀ̸¸Å­ Ãâ·Â if(tree != nil) { display(tree->left); printf("´Ü¾î:%s ",tree->key.word); printf("¶æ:%s",tree->key.meaning); printf("´Ü¾î °Ë»ö Ƚ¼ö: %d\n",tree->key.count); tree->key.count++; display(tree->right); } } void print_inorder(RBTREE tree,RBTREE entree){// INORDER ·Î Ž»öÇϸ鼭 ±íÀ̸¸Å­ Ãâ·Â DATA temp0; if(tree != nil) { print_inorder(tree->left,entree); if(print_count <= 0)return; print_count--; printf("´Ü¾î:%s ",tree->key.word); printf("¶æ:%s ",tree->key.meaning); printf("´Ü¾î °Ë»ö Ƚ¼ö: %d\n",tree->key.count); tree->key.count++; strcpy(temp0.word,tree->key.word); tree_searchword(entree,temp0)->key.count++; print_inorder(tree->right,entree); } } void print_inorder_word(RBTREE tree,DATA mindata,DATA maxdata){ if(tree != nil) { print_inorder_word(tree->left,mindata,maxdata); if((strcmp(tree->key.word,mindata.word) > 0)&&(strcmp(tree->key.word,maxdata.word) < 0)){ printf("´Ü¾î:%s ",tree->key.word); printf("¶æ:%s",tree->key.meaning); printf("´Ü¾î °Ë»ö Ƚ¼ö: %d\n",tree->key.count); } print_inorder_word(tree->right,mindata,maxdata); } } void print_inorderdate(RBTREE tree,RBTREE entree,int date){// INORDER ·Î Ž»öÇϸ鼭 ±íÀ̸¸Å­ Ãâ·Â DATA temp0; if(tree != nil) { print_inorderdate(tree->left,entree, date); if((date <= tree->key.date) && (tree->key.date < (date + 1000))){ printf("no.%d ",tree->key.date - date); printf("´Ü¾î:%s ",tree->key.word); printf("¶æ:%s\n",tree->key.meaning); strcpy(temp0.word,tree->key.word); tree_searchword(entree,temp0)->key.count++; } print_inorderdate(tree->right,entree, date); } } void print_inorder_end(RBTREE tree,FILE *f){// INORDER ·Î Ž»öÇϸ鼭 ±íÀ̸¸Å­ Ãâ·Â if(tree != nil) { print_inorder_end(tree->left, f); fprintf(f,"%d %s %s %d\n",tree->key.date,tree->key.word,tree->key.meaning,tree->key.count); print_inorder_end(tree->right,f); } } void delete_all(RBTREE tree){ if(tree != nil) { delete_all(tree->left); delete_all(tree->right); free(tree); } } void insert_max_heap(Heaptype *h,DATA item){ int i; i = ++(h->heap_size); while((i != 1)&&(item.count > h->heap[i/2].count)){ h->heap[i] = h->heap[i/2]; i /=2; } h->heap[i] = item; } void insert_min_heap(Heaptype *h,DATA item){ int i; i = ++(h->heap_size); while((i != 1)&&(item.count< h->heap[i/2].count)){ h->heap[i] = h->heap[i/2]; i /=2; } h->heap[i] = item; } DATA delete_max_heap(Heaptype *h){ int parent, child; DATA item, temp; item = h->heap[1]; temp = h->heap[(h->heap_size)--]; parent = 1; child = 2; while(child <= h->heap_size){ if((child < h->heap_size)&& (h->heap[child].count) < (h->heap[child+1].count)){ child++; } if(temp.count >= h->heap[child].count)break; h->heap[parent] = h->heap[child]; parent = child; child *= 2; } h->heap[parent] = temp; return item; } DATA delete_min_heap(Heaptype *h){ int parent, child; DATA item, temp; item = h->heap[1]; temp = h->heap[(h->heap_size)--]; parent = 1; child = 2; while(child <= h->heap_size){ if((child < h->heap_size)&& (h->heap[child].count) > (h->heap[child+1].count)){ child++; } if(temp.count <= h->heap[child].count)break; h->heap[parent] = h->heap[child]; parent = child; child *= 2; } h->heap[parent] = temp; return item; } void heap_init(Heaptype *minheap,Heaptype *maxheap){ minheap->heap_size = 0; maxheap->heap_size = 0; }